#include<iostream>
#include<sys/time.h>
//#include<windows.h>
using namespace std;

const static int SIZ = 2048;
double Result[2][SIZ] = { 0 };
double MatriN[SIZ][SIZ] = { 0 };
double VectorN[SIZ] = { 0 };
template<class T> bool equal(T* a, T* b);

int main()
{
	struct timeval sign1, sign2, sign3;
	//long long head, mid, end, freq;
	//long long t1, t2;
	//QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
	for (int i = 0; i < SIZ; i += 1)
	{
		for (int j = 0; j < SIZ; j += 1)
		{
			MatriN[i][j] = i + j + 0.0;
		}
		VectorN[i] = i + 0.0;
	}

	//QueryPerformanceCounter((LARGE_INTEGER*)&head);
	gettimeofday(&sign1, NULL);
	for(int o=0;o<1000;o+=1)
	for (int i = 0; i < SIZ; i += 1)
	{
		for (int j = 0; j < SIZ; j += 1)
		{
			Result[0][i] += MatriN[i][j] * VectorN[j];//平凡算法
		}
	}
	//QueryPerformanceCounter((LARGE_INTEGER*)&mid);
	gettimeofday(&sign2, NULL);
	for(int o=0;o<1000;o+=1)
	for (int i = 0; i < SIZ; i += 1)
	{
		for (int j = 0; j < SIZ; j += 1)
		{
			Result[1][j] += MatriN[i][j] * VectorN[i];//告急算法
		}
	}
	//QueryPerformanceCounter((LARGE_INTEGER*)&end);
	//
	gettimeofday(&sign3, NULL);
	cout << "平凡算法时间/us： " 
		<< (sign2.tv_sec*1000*1000+sign2.tv_usec)
			-(sign1.tv_sec*1000*1000+sign1.tv_usec);
	cout << " 告急算法时间/us： " 
		<< (sign3.tv_sec*1000*1000+sign3.tv_usec)
			-(sign2.tv_sec*1000*1000+sign2.tv_usec);
	cout << "\nJudgement: " << equal(Result[0], Result[1]) << '\n';
	for (int i = 0; i < 2; i += 1)
	{
		for (int j = 0; j < SIZ; j += 1)
		{
			printf("%lf ", Result[i][j]);
		}
		cout << '\n';
	}
	
	return 0;
}


template<class T>
bool equal(T* a, T* b)
{
	for (int i = 0; i < SIZ; i += 1)
	{
		if (a[i] != b[i])
			return false;
	}
	return true;
}
